import abc
import os

from library_test.base_test import BaseLibraryTest


class TestDLPrepareJob(BaseLibraryTest, metaclass=abc.ABCMeta):
    TESTCASE_DIR = os.path.join(os.path.dirname(__file__), "../testcase")

    @classmethod
    def get_module_path(cls):
        return "ascend_deployer.library.prepare_for_dl"

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        from ascend_deployer.library.prepare_for_dl import DLPrepareJob
        cls.dl_prepare_job = DLPrepareJob()

    @classmethod
    def get_testcase_path(cls):
        return os.path.join(cls.TESTCASE_DIR, "prepare_for_dl_success.yml")


class TestBaseModule(TestDLPrepareJob):

    def test_extract_indices(self):
        out = """NAME       STATUS   ROLES    AGE   VERSION   INTERNAL-IP   ...   KERNEL-VERSION      CONTAINER-RUNTIME
master-1   Ready    master   10d   v1.20.0   10.0.0.1      ...   5.4.0-42-generic    docker://19.3.13
worker-1   Ready    worker   10d   v1.20.0   10.0.0.2      ...   5.4.0-42-generic    docker://19.3.13
worker-2   Ready    worker   10d   v1.24.0   10.0.0.3      ...   5.4.0-42-generic    containerd://1.4.3"""
        # str
        self.assertEqual((0, -1), self.dl_prepare_job.extract_indices(out))

    def test_query_container_runtime_type(self):
        self.dl_prepare_job.query_container_runtime_type()
        # default
        self.assertEqual(self.dl_prepare_job.facts["container_runtime_type"],
                         {"master-1": "docker", "worker-1": "docker", "worker-2": "containerd"})
